package com.swt.gap.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.swt.gap.dao.OntologyNodeDao;
import com.swt.gap.dao.OntologyNodePropertyDao;
import com.swt.gap.dao.TaskSubscribeRuleDao;
import com.swt.gap.model.entity.TaskSubscribeRule;
import com.swt.gap.model.dto.SubscribeRuleDTO;
import com.swt.gap.model.dto.TaskSubscribeDTO;
import com.swt.gap.service.TaskSubscribeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sx
 */
@Service
public class TaskSubscribeServiceImpl implements TaskSubscribeService {

    private final TaskSubscribeRuleDao taskSubscribeRuleDao;
    private final OntologyNodeDao ontologyNodeDao;
    private final OntologyNodePropertyDao ontologyNodePropertyDao;

    public TaskSubscribeServiceImpl(TaskSubscribeRuleDao taskSubscribeRuleDao,
                                    OntologyNodeDao ontologyNodeDao,
                                    OntologyNodePropertyDao ontologyNodePropertyDao) {
        this.taskSubscribeRuleDao = taskSubscribeRuleDao;
        this.ontologyNodeDao = ontologyNodeDao;
        this.ontologyNodePropertyDao = ontologyNodePropertyDao;
    }


    @Override
    public Integer addTaskSubscribe(TaskSubscribeDTO taskSubscribeDTO) {
        // 一个任务只保留一个订阅规则
        taskSubscribeRuleDao.deleteByTaskId(taskSubscribeDTO.getTaskId());
        TaskSubscribeRule taskSubscribeRule = TaskSubscribeRule.builder()
                .taskId(taskSubscribeDTO.getTaskId())
                .rule(JSONArray.parseArray(JSON.toJSONString(taskSubscribeDTO.getRules())))
                .build();
        return taskSubscribeRuleDao.insertSelective(taskSubscribeRule);
    }

    @Override
    public List<SubscribeRuleDTO> getTaskSubscribe(Integer taskId) {
        List<SubscribeRuleDTO> retValue = new ArrayList<>();
        List<TaskSubscribeRule> taskSubscribeRules = taskSubscribeRuleDao.selectByTaskId (taskId);
        if (null != taskSubscribeRules && !taskSubscribeRules.isEmpty()) {
            JSONArray rules = taskSubscribeRules.get(0).getRule();
            retValue = JSONObject.parseArray(rules.toJSONString(), SubscribeRuleDTO.class);
            retValue.forEach(subscribeRule -> {
                String labels = ontologyNodeDao.selectByPrimaryKey(subscribeRule.getOntologyNodeId()).getLabels();
                subscribeRule.setOntologyNodeName(labels.substring(labels.indexOf(",") + 1));
                subscribeRule.getElements().forEach(element -> {
                    if (element.getType() == 1) {
                        String relNodeLabels = ontologyNodeDao.selectByPrimaryKey(element.getElementId()).getLabels();
                        element.setElement(relNodeLabels.substring(relNodeLabels.indexOf(",") + 1));
                    } else if (element.getType() == 2) {
                        element.setElement(ontologyNodePropertyDao.selectByPrimaryKey(element.getElementId()).getPropertyKey());
                    }
                });
            });
        }
        return retValue;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateTaskSubscribe(TaskSubscribeDTO taskSubscribeDTO) {
        int num = taskSubscribeRuleDao.deleteByTaskId(taskSubscribeDTO.getTaskId());
        if(!CollectionUtils.isEmpty(taskSubscribeDTO.getRules())) {
            TaskSubscribeRule taskSubscribeRule = TaskSubscribeRule.builder()
                    .taskId(taskSubscribeDTO.getTaskId())
                    .rule(JSONArray.parseArray(JSON.toJSONString(taskSubscribeDTO.getRules())))
                    .build();
            num = taskSubscribeRuleDao.insertSelective(taskSubscribeRule);
        }
        return num;
    }
}
